package com.zzzgroup;

import android.os.Handler;
import android.os.SystemClock;
import android.os.CountDownTimer;

public class Timer implements Runnable {
	private PomodoroTime _currentTime;
	private PomodoroTime _initTime;
	private PomodoroTimerEventHandler _tickEventListener;

	private Handler mHandler = new Handler();

	private long mStartTime = 0L;

	private boolean _isStarted = false;

	private CountDownTimer _workTimetimer;
	private CountDownTimer _restTimetimer;

	private final boolean _isAutoTimerStart = false;

	public Timer(PomodoroTime time, PomodoroTimerEventHandler tickEventListener) {
		_initTime = time;
		_currentTime = new PomodoroTime(_initTime.getWorkTime(),
				_initTime.getRestTime());
		_tickEventListener = tickEventListener;

		// CreateTimers(_initTime);
	}

	private void CreateTimers(PomodoroTime time) {
		_workTimetimer = new CountDownTimer(time.getWorkTime() * 1000, 1000) {

			@Override
			public void onTick(long millisUntilFinished) {
				_currentTime.SetWorkTime(millisUntilFinished / 1000);
				Tick();
			}

			@Override
			public void onFinish() {
				WorkTimeFinished();

			}
		};

		_restTimetimer = new CountDownTimer(time.getRestTime() * 1000, 1000) {

			@Override
			public void onTick(long millisUntilFinished) {
				_currentTime.SetRestTime(millisUntilFinished / 1000);
				Tick();
			}

			@Override
			public void onFinish() {
				RestTimeFinished();

			}
		};

	}

	public void StartRestTimer() {
		if (!_isAutoTimerStart)
			_restTimetimer.start();
	}

	public void StartWorkTimer() {
		if (!_isAutoTimerStart)
			_workTimetimer.start();
	}

	private void WorkTimeFinished() {
		_workTimetimer.cancel();

		if (_isAutoTimerStart)
			_restTimetimer.start();
		_currentTime.SetRestTime(_initTime.getRestTime());
		_currentTime.SetWorkTime(0);
		if (_tickEventListener != null)
			_tickEventListener.WorkTimeEnded();
	}

	private void RestTimeFinished() {
		_restTimetimer.cancel();
		if (_isAutoTimerStart)
			_workTimetimer.start();
		_currentTime.SetWorkTime(_initTime.getWorkTime());
		_currentTime.SetRestTime(0);
		if (_tickEventListener != null)
			_tickEventListener.RestTimeEnded();
	}

	private void Tick() {
		if (_tickEventListener != null)
			_tickEventListener.Tick(_currentTime);
	}

	@Override
	public void run() {
		final long start = SystemClock.uptimeMillis();

		/*
		 * if (_currentTime.getWorkTime()>0) {
		 * _currentTime.DecrementWorkTimeBySecond(); if
		 * (_currentTime.getWorkTime()==0) {
		 * _currentTime.SetRestTime(_initTime.getRestTime()); if
		 * (_tickEventListener!=null) _tickEventListener.WorkTimeEnded(); } }
		 * else { //time to rest if (_currentTime.getRestTime()>0) {
		 * _currentTime.DecrementRestTimeBySecond(); if
		 * (_currentTime.getRestTime()==0) {
		 * _currentTime.SetWorkTime(_initTime.getWorkTime()); if
		 * (_tickEventListener!=null) _tickEventListener.RestTimeEnded(); } } }
		 * 
		 * 
		 * if (_tickEventListener!=null) _tickEventListener.Tick(_currentTime);
		 * 
		 * mHandler.postAtTime(this, start + 1000);
		 */
	}

	private CountDownTimer getCurrentTimer() {
		if (_currentTime.getWorkTime() > 0)
			return _workTimetimer;
		else
			return _restTimetimer;
	}

	public void Start() {
		/*
		 * if (mStartTime == 0L) { mStartTime = SystemClock.uptimeMillis();
		 * mHandler.removeCallbacks(this); mHandler.postDelayed(this, 1000);
		 * _isStarted = true; }
		 */

		CreateTimers(_currentTime);
		getCurrentTimer().start();

		_isStarted = true;
	}

	public void Stop() {
		// mHandler.removeCallbacks(this);
		// mStartTime = 0L;
		if (_isStarted)
			getCurrentTimer().cancel();
		_isStarted = false;
	}

	public void setTime(PomodoroTime time) {
		Stop();
		_initTime = time;
		_currentTime = new PomodoroTime(_initTime.getWorkTime(),
				_initTime.getRestTime());
		CreateTimers(time);
		if (_tickEventListener != null) {
			_tickEventListener.TimeSet(_currentTime);
		}
	}

	public void Reset() {
		Stop();
		_currentTime = new PomodoroTime(_initTime.getWorkTime(),
				_initTime.getRestTime());

		if (_tickEventListener != null) {
			_tickEventListener.TimerReseted(_currentTime);
		}
	}

	public PomodoroTime getCurrentTime() {
		return _currentTime;
	}

	public boolean getIsStarted() {
		return _isStarted;
	}

}
